package core

type DBMap struct {
	Keys   []string
	Values []*Collection
}

func NewDBMap() *DBMap {
	return &DBMap{
		Keys:   []string{},
		Values: []*Collection{},
	}
}

func (t *DBMap) Put(key string, value *Collection) {
	pos := t.indexOf(key)
	if pos > -1 {
		t.Keys[pos] = key
		t.Values[pos] = value
	} else {
		t.Keys = append(t.Keys, key)
		t.Values = append(t.Values, value)
	}

}

func (t *DBMap) Get(key string) *Collection {
	pos := t.indexOf(key)
	if pos > -1 {
		return t.Values[pos]
	}
	return nil
}

func (t *DBMap) Delete(key string) bool {
	pos := t.indexOf(key)
	if pos == -1 {
		return false
	}
	t.Keys = append(t.Keys[:pos], t.Keys[pos+1:]...)
	t.Values = append(t.Values[:pos], t.Values[pos+1:]...)
	return true
}

func (t *DBMap) indexOf(key string) int {
	for i, v := range t.Keys {
		if v == key {
			return i
		}
	}
	return -1
}

func (t *DBMap) Len() int {
	return len(t.Keys)
}
